<?php

/**
 * This is the model class for table "hd_warranty_fixing".
 *
 * The followings are the available columns in table 'hd_warranty_fixing':
 * @property string $id
 * @property string $user_id
 * @property string $department
 * @property string $received_date
 * @property string $detail
 * @property string $note
 * @property integer $is_deleted
 *
 * The followings are the available model relations:
 * @property User $user
 */
class WarrantyFixing extends CActiveRecord
{
    public $date1;
    public $date2;
	/**
	 * Returns the static model of the specified AR class.
	 * @return WarrantyFixing the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'hd_warranty_fixing';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('is_deleted', 'numerical', 'integerOnly'=>true),
			array('user_id', 'length', 'max'=>10),
			array('department', 'length', 'max'=>256),
			array('department,detail,received_date','required'),
			array('received_date', 'type', 'type'=>'date'),
			array('received_date, detail, note', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, department, received_date, detail, note, is_deleted', 'safe', 'on'=>'search'),
                        array('date1 , date2', 'safe', 'on'=>'searchByTime'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'department' => 'Phòng ban',
			'received_date' => 'Ngày tiếp nhận',
			'detail' => 'Nội dung công việc',
			'note' => 'Ghi chú',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->condition = " is_deleted=:is_deleted and user_id=:user_id";  
		$criteria->params = array(':is_deleted'=>Common::getUnDeletedCode(), ':user_id'=>Yii::app()->user->id);
		$criteria->order = 'received_date asc';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
         //Search Job by time 
	public function searchByTime(array $time = NULL)
	{
		$criteria=new CDbCriteria;
		if(isset($time['date1']) && isset($time['date2']))
		{
			$date1 = date("Y-m-d" , strtotime($time['date1']));
			$date2 = date("Y-m-d" , strtotime($time['date2']));	
			$criteria->order = 'received_date asc';
                        $criteria->compare('is_deleted', Common::getUnDeletedCode());
                       // $criteria->compare('user_id', Yii::app()->user->id);
                        $user_id = Yii::app()->user->id;
			$criteria->condition = "user_id = '$user_id' AND is_deleted = 0 AND received_date BETWEEN '$date1'  AND '$date2'";
			//$criteria->addBetweenCondition('received_time', $date1 , $date2);
		}
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        //Get listJob By time
	public function getListWarrantyFixingByTime($date1 , $date2)
	{
		$date1 = date("Y-m-d" , strtotime($date1));
		$date2 = date("Y-m-d" , strtotime($date2));
                $user_id = Yii::app()->user->id;
		$listWarrantyFixing = Yii::app()->db->createCommand()
			->select('wf.department , wf.received_date ,  wf.detail ,
				wf.note' )
			->from('hd_warranty_fixing wf')				
			->where("user_id = '$user_id' AND is_deleted = 0 AND received_date BETWEEN '$date1'  AND '$date2' ")
			->order('received_date ASC')
			->queryAll();
		return $listWarrantyFixing;
	}
	
        
        
	public function deleteEx(){
		$this->setAttribute('is_deleted', 1);
		$this->update(array('is_deleted'));
	}
}